<?php

namespace app\index\controller;

use app\index\service\Book;
use app\index\service\Client;
use app\index\service\DataBase;
use app\index\service\EBookAdapter;
use app\index\service\Event;
use app\index\service\FactoryProduct;
use app\index\abstracts\Registry;
use app\index\service\Failure;
use app\index\service\Femaleuser;
use app\index\service\Kindle;
use app\index\service\MaleUser;
use app\index\service\Man;
use app\index\service\Mediator;
use app\index\service\Observer1;
use app\index\service\Observer2;
use app\index\service\Relationship;
use app\index\service\Server;
use app\index\service\Single;
use app\index\service\Success;
use app\index\service\Visitor;
use app\index\service\Woman;

class Index
{
    //1单例模式
    public function single()
    {
        $instance = Single::getInstance();
        $instance->say();
    }

    //2简单工厂
    public function factory()
    {
        $factory       = new FactoryProduct();
        $shipp_product = $factory->createDigitalProduct(2000)->calculatePrice();
        $digital       = $factory->createShippableProduct(1500, 300)->calculatePrice();
        print_r('ShippingProduct:' . $shipp_product);
        echo '<br/>';
        print_r('Digital:' . $digital);
    }

    //3注册模式
    public function registry()
    {
        $key = Registry::LOGGER;
        Registry::set($key, '注册模式测试!');
        echo Registry::get($key);
    }

    //4适配器
    public function adapter()
    {
        $book = new Book();
        $book->open();
        $book->turnPage();
        echo $book->getPage();

        $kindle = new Kindle();
        echo '<hr/>';
        $ebook = new EBookAdapter($kindle);
        $ebook->open();
        $ebook->turnPage();
        echo $ebook->getPage();
    }

    //5策略模式
    public function strategy()
    {
        $strategy = ($_GET['gender'] === 1) ? new MaleUser() : new Femaleuser();
        echo $strategy->showAd();
        echo '<br/>';
        echo $strategy->showCategory();
    }

    //6观察者模式
    public function observer()
    {
        $event = new Event();
        $event->addObserver(new Observer1());
        $event->addObserver(new Observer2());
        $event->trigger();
        $event->notify();
    }

    //7中介者模式
    public function mediator()
    {
        $client = new Client();
        new Mediator(new DataBase(), $client, new Server());

        echo $client->request();

    }

    //8访问者模式
    public function visitor(){
        $visitor = new Visitor();
        $visitor->add(new Man());
        $visitor->add(new Woman());

        //成功状态
        $success = new Success();

        //失败状态
        $failure = new Failure();

        //恋爱状态
        $relationship = new Relationship();

        //显示
        $visitor->display($success);
        $visitor->display($failure);
        $visitor->display($relationship);
    }

    //redis 锁测试
    public function redisKeyLock(){
        $lock_id = RedisLock::addLock('19120903');
        echo 'Redis锁ID：'.$lock_id.'<br/>';
        $lock_release = RedisLock::releaseLock('19120903',$lock_id);
        echo 'Redis解锁：'.$lock_release;
    }

    //算法调试
    public function mathematics(){
        $arr = [2, 13, 42, 34, 56, 23, 67, 365, 87665, 54, 68, 3];
        $math = new Mathematics();
        $result = $math->quick_sort($arr);
        print_r($result);
    }
}
